package com.polaris.live.dialog

import android.content.Context
import android.view.View
import com.bumptech.glide.Glide
import com.lxj.xpopup.XPopup
import com.lxj.xpopup.core.CenterPopupView
import com.polaris.live.R
import com.polaris.live.common.ext.setOnSingleClickListener
import com.polaris.live.databinding.DialogToolCommonBinding

/**
 * ToolCommonDialog 道具通用弹窗1
 *
 * @author Created by 天晴 on 2024/7/27/016 19:40
 */
class ToolCommonDialog(
    context: Context,
    private val mDetail: String,
    private val mImageUrl: String? = null,
    private val mTvCanal: String? = null,
    private val mOnCanal: (() -> Unit)? = null,
    private val mOnSure: () -> Unit,
) : CenterPopupView(context) {

    override fun getImplLayoutId(): Int {
        return R.layout.dialog_tool_common
    }

    private lateinit var mBinding: DialogToolCommonBinding

    override fun onCreate() {
        super.onCreate()
        mBinding = DialogToolCommonBinding.bind(popupImplView)

        mBinding.tvDetail.text = mDetail
        if (mImageUrl.isNullOrEmpty()) {
            mBinding.ivImage.visibility = View.GONE
        } else {
            Glide.with(context).load(mImageUrl).into(mBinding.ivImage)
        }

        if (mTvCanal?.isNotEmpty() == true) {
            mBinding.tvQx.text = mTvCanal
        }

        mBinding.tvQx.setOnSingleClickListener {
            dismiss()
            mOnCanal?.invoke()
        }
        mBinding.tvConfirm.setOnSingleClickListener {
            dismiss()
            mOnSure.invoke()
        }
    }

    companion object {

        fun show(
            context: Context,
            detail: String,
            imageUrl: String? = null,
            tvCanal: String? = null,
            onCanal: (() -> Unit)? = null,
            onSure: () -> Unit,
        ) {
            XPopup.Builder(context)
                .isDestroyOnDismiss(true)
                .asCustom(ToolCommonDialog(context, detail, imageUrl, tvCanal, onCanal, onSure))
                .show()
        }
    }
}